home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 234_01 / getapage.c < prev    next >
Text File  |  1987-06-16  |  12KB  |  448 lines

  1. /*
  2.   HEADER: CUG     nnn.nn;
  3.   TITLE:     XDIR - Hard Disk Manager
  4.   VERSION:     1.0 for IBM-PC
  5.   DATE:      Apr 03, 1987
  6.   DESCRIPTION:     Hard Disk Manager for IBM PC
  7.   KEYWORDS:     Hard Disk Manager Dump Directory
  8.   SYSTEM:     IBM-PC and Compatiables
  9.   FILENAME:      getapage.c
  10.   WARNINGS:     None
  11.   CRC:         N/A
  12.   SEE-ALSO:     HDIR.DOC and XDIR.DOC
  13.   AUTHOR:     Mike Blakley 15645 SW 82 Cir Ln #76, Miami, Fl 33193
  14.   COMPILERS:     ECO-C
  15.   REFERENCES:     XDIR.DOC
  16. */
  17.  
  18.  
  19. /*
  20.    getapage
  21.    get a page at a time from a disc file
  22.    used in the routine XDIR to display files
  23. */
  24. #include "stdio.h"
  25. /* #define DEBUG 1  */
  26. #define MAXPAGE 1000
  27. #define PAGESIZE 2000
  28. /*
  29.    typ
  30.    type a file to the console
  31.  
  32. */
  33. void typ(fnam)
  34. char *fnam;
  35. {
  36.      static int  i,j,k,l,len;
  37.      static int  found,m,c;
  38.      static int  nflag;
  39.      static int  fd;
  40.      static int  sflag, oldi;
  41.      static int  currpage, lastpage;
  42.      static int  row, col;
  43.      static char temp[80];
  44.      static char temp1[80];
  45.      static char sword[80];       /* search word */
  46.      static char xtemp[PAGESIZE]; /* page buffer */
  47.      static char xsave[PAGESIZE]; /* page buffer */
  48.      struct {
  49.         long offs;        /* offset in file */
  50.         int  plen;        /* length of page */
  51.         } pages[MAXPAGE];
  52.      long foff;
  53.  
  54.      k = l = len = 0;
  55.      i = j = 1;
  56.      foff = 0L;
  57.  
  58.      while (1)     /* build array of file info */
  59.      {
  60.      j = getxline(temp,80,fnam,i);
  61.      if (j == -1) break;
  62.      i = 0;
  63.      len += j;    /* update length */
  64.      ++l;         /* line counter */
  65.      if ((l%23) == 0)
  66.         {
  67.         pages[k].offs = foff;
  68.         pages[k].plen = len;
  69.         foff += (long) len;
  70.         len = 0;
  71.         ++k;
  72.         if (k >= MAXPAGE) {--k;break;}
  73.         }
  74.      }
  75.  
  76.      if (len > 0)
  77.        {
  78.        pages[k].offs = foff;    /* update last page */
  79.        pages[k].plen = len;
  80.        ++k;
  81.        }
  82.      lastpage = k;              /* note the last page */
  83.  
  84. #ifdef DEBUG
  85.      for (foff=0L,i=0;i<k;i++)      /* list the file info */
  86.      {
  87.      foff += (long) pages[i].plen;
  88.      }
  89.      printf("\nSize of %s is %ld",fnam,foff);
  90.      writestr("\nPress any key ... ");
  91.      getch();
  92.  
  93.      for (i=c=0;i<k;i++)              /* list the detail */
  94.      {
  95.      printf("\nPage %d offset %ld length %d",(i+1),pages[i].offs,
  96.            pages[i].plen);
  97.      if ((i%20) == 19)
  98.         {
  99.         writestr("\nNext page or X=cancel ");
  100.         c=getch();
  101.         }
  102.      if (c == 'X') break;
  103.      }
  104. #endif
  105.  
  106.      fd = open(fnam,0);
  107.      if (fd == -1) {writestr("\nCan't open ");
  108.                     writestr(fnam);
  109.                     exit(0);}
  110.  
  111.  
  112.      currpage = sflag = 0;              /* begin with page 0 */
  113.      while (1)
  114.      {
  115.      if (sflag == 0)
  116.      {
  117.      top(fnam,currpage);
  118.      foff = lseek(fd,pages[currpage].offs,0);
  119.      j = pages[currpage].plen;
  120.      if (j >= PAGESIZE) j = PAGESIZE;     /* prevent overflow */
  121.      read(fd,xtemp,j);
  122.      j -= 2;
  123.      xtemp[j] = 0;
  124.      writestr(xtemp);
  125.      bot();          /* write the bottom */
  126.      }
  127.      sflag = 0;
  128.      c =toupper(getch());
  129.      if (c == 0) c=getch();
  130.  
  131.      nflag = 0;        /* no numeric input */
  132.      switch (c)
  133.      {
  134.      case '\033':      /* escape */
  135.         break;
  136.      case 0x0d:
  137.      case 0x0a:
  138.         ++currpage;
  139.         break;
  140.      case 73:         /* page up */
  141.         --currpage;
  142.         break;
  143.      case 81:         /* page down */
  144.         ++currpage;
  145.         break;
  146.      case 71:         /* home */
  147.         currpage = 0;
  148.         break;
  149.      case 79:         /* end */
  150.         currpage = lastpage-1;
  151.         break;
  152.      case '+':
  153.      case '-':
  154.      case '0':
  155.      case '1':
  156.      case '2':
  157.      case '3':
  158.      case '4':
  159.      case '5':
  160.      case '6':
  161.      case '7':
  162.      case '8':
  163.      case '9':
  164.        putchar(c);    /* display it */
  165.        nflag = 1;
  166.        break;
  167.      case 59:         /* F1 = help */
  168.         clrscr();
  169.         writestr("Options are as follows: \n");
  170.         writestr("\nOption       Description");
  171.         writestr("\nPgUp         Displays the previous page");
  172.         writestr("\nPgDn         Displays the following page ");
  173.         writestr("\nHome         Displays the first page ");
  174.         writestr("\nEnd          Displays the last page ");
  175.         writestr("\n+nnn         Page forward nnn pages from here ");
  176.         writestr("\n-nnn         Page backward nnn pages from here ");
  177.         writestr("\nnnn          Go to absolute page nnn ");
  178.         writestr("\nEnter        Next page ");
  179.         writestr("\nF1           Help  - This page ");
  180.         writestr("\nS<string>    Search for text <string>");
  181.         writestr("\nInvalid key  Causes beep sound");
  182.         writestr("\n\n\nPress any key to continue");
  183.         getch();
  184.         break;
  185.      case 60:    /* F2 - display page layouts */
  186.         clrscr();
  187.         for (m=0,row=col=1;m<lastpage;m++)
  188.         {
  189.         cursor(row,col);
  190.         writestr("Pg ");
  191.         itoa(temp1,(m+1));
  192.         writestr(temp1);
  193.         writestr(" offs ");
  194.         ltoa(temp1,pages[m].offs);
  195.         writestr(temp1);
  196.         writestr(" len ");
  197.         itoa(temp1,pages[m].plen);
  198.         writestr(temp1);
  199.         ++row;
  200.         if (row > 23)
  201.            {
  202.            row = 1;
  203.            col +=25;
  204.            }
  205.         }
  206.         getch();
  207.         break;
  208.      case 'S':   /* search for text string */
  209.            sflag = 1;         /* set search flag */
  210.            break;
  211.      default:
  212.         putchar('\007');   /* beep */
  213.         break;
  214.      }          /* end case */
  215.  
  216.      if (c == '\033') break;     /* end of program */
  217.  
  218.      if (nflag)   /* numeric input */
  219.         {
  220.         gets(temp);      /* get numeric */
  221.         j = atoi(temp);
  222.         if (c == '+') currpage += j;
  223.         else if (c == '-') currpage -= j;
  224.         else {
  225.              temp1[0] = c;
  226.              temp1[1] = 0;
  227.              strcat(temp1,temp);
  228.              j = atoi(temp1);
  229.              currpage = (j-1);
  230.              }
  231.         }                /* end if numeric */
  232.  
  233.       if (c == 'S')      /* search string */
  234.         {
  235.         int  c1;
  236.         char *cpo;
  237.         cpo = temp;
  238.         oldi = currpage;          /* save page number */
  239.         putchar(c);
  240.         writestr("earch? ");
  241.  
  242.         while (1)
  243.         {
  244.         c1 = getch();
  245.         if (c1 == '\033') break;
  246.         if (c1 == '\015') break;
  247.         putchar(c1);
  248.         *cpo++ = c1;
  249.         }
  250.         *cpo = 0;
  251.  
  252.         l = strlen(temp);
  253.         if (c1 == '\033') break;      /* escape = no search */
  254.         if (l != 0)                   /* default previous text */
  255.             strcpy(sword,temp);
  256.  
  257.         for (;currpage<lastpage;currpage++)  /* search from current page forward */
  258.         {
  259.         int  n;
  260.         foff = lseek(fd,pages[currpage].offs,0);
  261.         j = pages[currpage].plen;
  262.         read(fd,xtemp,j);
  263.         j -= 2;
  264.         xtemp[j] = 0;
  265.  
  266.            for (m=found=0;m<j;m++)         /* search the buffer */
  267.            {
  268.            if ((n=strncmp(sword,xtemp+m,l)) == 0)
  269.               {found=1;break;}
  270.            }
  271.  
  272.            if (found)  break;             /* stop at first occurence */
  273.          }    /* end for (;i<k) */
  274.  
  275.         if (found)
  276.               {
  277.               top(fnam,currpage);
  278.               strncpy(xsave,xtemp,m);
  279.               xsave[m] = 0;
  280.               writestr(xsave);
  281.               writestr("\033[7m");
  282.               writestr(sword);
  283.               writestr("\033[0m");
  284.               strcpy(xsave,xtemp+m+strlen(sword));
  285.               writestr(xsave);
  286.               bot();
  287.               }
  288.         else
  289.              {
  290.              writestr(" Not found ");
  291.              currpage = oldi;
  292.              }
  293.         }      /* end if c == 'S' */
  294.  
  295.      if (currpage >= lastpage) currpage = lastpage-1;   /* max */
  296.      if (currpage < 1) currpage = 0;
  297.  
  298.      }       /* end while 1 */
  299.  
  300.      close(fd);
  301.  
  302.  
  303. }            /* end function typ */
  304.  
  305. getxline(buffer,maxlen,fnam,action)
  306. char *buffer;       /* output buffer */
  307. int  maxlen;